﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QuanLyShop.DTO;
using System.Data.OleDb;
using DataAcessLayer;
using System.Data.Common;

namespace QuanLyShop.DAO
{
   public class NguoiDungDAO
    {
        #region 1. Load danh sách người dùng
        public  DataTable LayDanhSachNguoiDung()
        {
            DataTable dt = null;
            try
            {
                DataProvider dp = new DataProvider();
                string sql = "select * from NGUOI_DUNG nd where nd.TINHTRANGXOA = 0";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;

                dt = dp.ExecuteSelectCommand(command);
            }
            catch (Exception e)
            {

                throw new Exception("Lỗi rồi" + e.ToString());
            }
            return dt;
            
        }
        #endregion

        #region 2. Thêm người dùng

        public int ThemNguoiDung(NguoiDungDTO nd)
        {
            int kq = 0;
            try
            {
                DataProvider dp = new DataProvider();
                String sql = "insert into NGUOI_DUNG(ID_NGUOIDUNG,TENDANGNHAP,MATKHAU,HOTENNGUOIDUNG,QUYENHAN,TINHTRANG,LOG, THONGTINTHEM, TINHTRANGXOA) values ('"+nd.ID_NguoiDung+"',N'"+nd.TenDangNhap+"','"+nd.MatKhau+"',N'"+nd.HoTenNguoiDung+"',N'"+nd.QuyenHan+"',N'"+nd.TinhTrang+"',N'"+nd.Log+"',N'"+nd.ThongTinThem+"',"+nd.TinhTrang+")";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                dp.ExecuteNonQueryCommand(command);
                kq = 1;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return kq;
        }
        #endregion


        #region 3. Cập nhật người dùng

        public int  CapNhatNguoiDUng(NguoiDungDTO nd)
        {
            int kq = 0;
            try
            {
                DataProvider dp = new DataProvider();
                String sql = "update NGUOI_DUNG set TENDANGNHAP=N'"+nd.TenDangNhap+"',MATKHAU='"+nd.MatKhau+"',HOTENNGUOIDUNG=N'"+nd.HoTenNguoiDung+"',QUYENHAN=N'"+nd.QuyenHan+"',TINHTRANG='"+nd.TinhTrang+"', LOG='"+nd.Log+"', THONGTINTHEM=N'"+nd.ThongTinThem+"', TINHTRANGXOA="+nd.TinhTrangXoa+" where ID_NGUOIDUNG='"+nd.ID_NguoiDung+"'";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                dp.ExecuteNonQueryCommand(command);
                kq = 1;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return kq;
            
        }
        #endregion
        #region 4. Mã tăng tự động

        public DataTable Lay()
        {

            DataTable dt = null;
            try
            {
                DataProvider dp = new DataProvider();
                string sql = "select ID_NGUOIDUNG from NGUOI_DUNG";
                DbCommand command = dp.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                dt = dp.ExecuteSelectCommand(command);

            }
            catch (Exception e)
            {

                throw new Exception("Lỗi rồi" + e.ToString());
            }
            return dt;
        }
        public string MaTangTuDong()
        {
            string str = "";
            PhatSinhMaTangTuDong l = new PhatSinhMaTangTuDong();
            DataTable dt = Lay();
            string c = l.LayChuoiNgayThangNam();
            if (dt.Rows.Count == 0)
            {
                str = c + "0001";
            }
            else
            {
                string a = dt.Rows[dt.Rows.Count - 1].ItemArray[0].ToString();
                string b = a.Substring(0, 6);
                if (b.Equals(c))
                {
                    string p = a.Substring(6);
                    int so = int.Parse(p);
                    so = so + 1;
                    if (so < 10)
                    {
                        str = "000" + so;

                    }
                    else if (so < 100)
                    {
                        str = "00" + so;

                    }
                    else if (so < 1000)
                    {
                        str = "0" + so;
                    }
                    str = c + str;
                }
                else
                {
                    str = c + "0001";
                }
            }
            return str;
        }
        #endregion
    }
}
